/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.service.dao.impl;

import com.wisnu.spms.service.dao.DbStudentDao;
import com.wisnu.spms.service.entity.DbStudent;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class DbStudentDaoImpl extends HibernateDaoSupport implements DbStudentDao {

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void insertStudent(DbStudent student) throws Exception {
        try {
            getHibernateTemplate().save(student);
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void updateStudent(DbStudent student) throws Exception {
        try {
            getHibernateTemplate().update(student);
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void deleteStudent(DbStudent student) throws Exception {
        try {
            getHibernateTemplate().delete(student);
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = true)
    public DbStudent getStudentById(final Integer id) throws Exception {
        try {
            Object o = getHibernateTemplate().execute(new HibernateCallback() {

                @Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    return session.createQuery("select a from DbStudent a where a.studentId = :id")
                            .setMaxResults(1).setParameter("id", id).uniqueResult();
                }
            });
            if (o == null) {
                //throw new Exception("Student with ID " + id + " do not found");
                return null;
            } else {
                return (DbStudent) o;
            }
        } catch (Exception t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<DbStudent> getStudents() throws Exception {
        try {
            return getHibernateTemplate().loadAll(DbStudent.class);
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<DbStudent> getStudentsByKeyword(final String keyword) throws Exception {
        try {
            Object o = getHibernateTemplate().execute(new HibernateCallback() {

                @Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    return session.createQuery("select a from DbStudent a WHERE a.studentName LIKE :keyword")
                            .setParameter("keyword", "%"+keyword+"%").list();
                }
            });
            if (o == null) {
                //throw new Exception("Student with ID " + id + " do not found");
                return null;
            } else {
                return (List<DbStudent>) o;
            }
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

}
